/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.util;

/**
 * StringDisplay is a utility class with operations of displaying string in
 * different formats
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class StringDisplay {
	/**
	 * Get blank if the input string is null
	 * 
	 * @param input
	 *            input string
	 * @return a non-null string
	 */
	public static String showString(String input) {
		if (input == null)
			return Symbols.BLANK;
		else
			return input;
	}

	/**
	 * Get a string with the designated size
	 * 
	 * @param input
	 *            input string
	 * @param size
	 *            designated size
	 * @return
	 */
	public static String showSizedString(String input, int size) {
		if (input == null || input.length() <= size)
			return input;
		else {
			return input.subSequence(0, size - 2) + "...";
		}
	}

	/**
	 * Escape tags in a string
	 * 
	 * @param input
	 *            input string
	 * @return a string which escaped tags
	 */
	public static String secureOutput(String input) {
		if (input == null)
			return Symbols.BLANK;
		String result = input.replace("<", "&lt");
		result = result.replace(">", "&gt");
		result = result.replace(Symbols.NEW_LINE_MARK, "<br/>");
		return result;

	}

	/**
	 * Get a string with a highlighted key
	 * 
	 * @param input
	 *            input string
	 * @param key
	 *            a string to be highlighted
	 * @return a string with a highlighted key
	 */
	public static String getStringWithRedKey(String input, String key) {
		StringBuffer stringBuffer = new StringBuffer();
		int start = 0;
		int temp = input.indexOf(key, start);

		while (temp != -1) {
			stringBuffer.append(input.substring(start, temp));
			stringBuffer.append("<font color=red>");
			stringBuffer.append(key);
			stringBuffer.append("</font>");
			start = temp + key.length();
			temp = input.indexOf(key, start);
		}
		stringBuffer.append(input.substring(start));

		return stringBuffer.toString();
	}

	/**
	 * Get a string buffer of reminder date
	 * 
	 * @param startDate
	 *            the start date
	 * @param endDate
	 *            the end date
	 * @return a string buffer of reminder date
	 */
	public static StringBuffer getReminderDateStringBuffer(String startDate,
			String endDate) {
		StringBuffer dateBuffer = new StringBuffer();

		if (startDate != null && !Symbols.BLANK.equals(startDate))
			dateBuffer.append(startDate);
		if (endDate != null & !Symbols.BLANK.equals(endDate)) {
			if (startDate != null && !startDate.equals(endDate))
				dateBuffer.append(Symbols.TILDE);
			if (!endDate.equals(startDate))
				dateBuffer.append(endDate);
		}
		return dateBuffer;
	}
}
